//
//  PopoverAnimate.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/3/22.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit
//修改按钮的选中状态
//声明的常亮是都可以用的
let XMGPopoverAnimatorWillShow = "XMGPopoverAnimatorWillShow"
let XMGPopoverAnimatorWilldismiss = "XMGPopoverAnimatorWilldismiss"


class PopoverAnimate: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    var isPresentControllerShow: Bool?
    var presentFrame = CGRect.zero
    //返回控制器跳转的对象
    //实现代理,谁来负责我们以后的专场动画
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
        let PopoverPresentationVC = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        PopoverPresentationVC.presentFrame = presentFrame
        return PopoverPresentationVC
    }
    
    //MARK:- 只要实现下面的方法 系统默认的动画就没有了 所有的东西都要你自己写
    
    /// 告诉系统谁来执行model的展现动画  展现视图出现时候会调用这个方法
    ///
    /// - Parameters:
    ///   - presented: 被展现视图
    ///   - presenting: 发起的视图
    ///   - source: <#source description#>
    /// - Returns: 谁来负责
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresentControllerShow = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: XMGPopoverAnimatorWillShow), object: nil)
        return self
    }
    
    /// 告诉系统谁来执行model小时的动画   展现视图消失的时候会执行这个方法
    ///
    /// - Parameter dismissed: 被关闭的视图
    /// - Returns: 谁来负责
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresentControllerShow = false
        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: XMGPopoverAnimatorWilldismiss), object: nil)
        return self
    }
    
    
    /// 动画执行的时间
    ///
    /// - Parameter transitionContext: 上下文(里面储存的动画所需要的所有信息)
    /// - Returns: 动画执行时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        return 0.5
    }
    
    /// 告诉系统 无论展现还是消失动画都会执行这个方法
    ///
    /// - Parameter transitionContext: 上下文
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        //1.拿到被展现的视图 (使用上下文来拿到展现的ViewController)
        //        let presentVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        //        let presentationVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        //可以通过打印知道那个是被展现的控制器  和 发起的控制器
        //        print(presentVC ?? 99)
        //        print(presentationVC ?? 999)
        
        if isPresentControllerShow! {
            print("展现")
            //我们可以直接拿到被展现的View
            let presentView = transitionContext.view(forKey: UITransitionContextViewKey.to)
            //视图的锚点默认是(0.5,0.5)所以动画是在视图的中心出来 我们要更改锚点的位子
            presentView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            //要把视图自己添加到上下文中
            transitionContext.containerView.addSubview(presentView!)
            //2.执行动画
            presentView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                presentView?.transform = CGAffineTransform.identity //恢复原来的样子
            }) { (_) in
                //你执行完动画一定要告诉系统
                //如果不写这个可能会发生一些未知的错误
                transitionContext.completeTransition(true)
            }
        }else{
            print("关闭")
            let presentationView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                // y: 0.0这样写  会没有动画  可能是CGFloat是不确定的填写0.0 可能没有动画
                presentationView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.000001)
            }, completion: { (_) in
                transitionContext.completeTransition(true);
            })
        }
    }
}
